/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;

import Dao.exceptions.NonexistentEntityException;
import Dao.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Logica.Deportista;
import Logica.Competencia;
import Logica.Compite;
import Logica.CompitePK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author User
 */
public class CompiteJpaController implements Serializable {

    public CompiteJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Compite compite) throws PreexistingEntityException, Exception {
        if (compite.getCompitePK() == null) {
            compite.setCompitePK(new CompitePK());
        }
        compite.getCompitePK().setIdCompetencia(compite.getCompetencia().getIdCompetencia());
        compite.getCompitePK().setIdDeportista(compite.getDeportista().getIdDeportista());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Deportista deportista = compite.getDeportista();
            if (deportista != null) {
                deportista = em.getReference(deportista.getClass(), deportista.getIdDeportista());
                compite.setDeportista(deportista);
            }
            Competencia competencia = compite.getCompetencia();
            if (competencia != null) {
                competencia = em.getReference(competencia.getClass(), competencia.getIdCompetencia());
                compite.setCompetencia(competencia);
            }
            em.persist(compite);
            if (deportista != null) {
                deportista.getCompiteCollection().add(compite);
                deportista = em.merge(deportista);
            }
            if (competencia != null) {
                competencia.getCompiteCollection().add(compite);
                competencia = em.merge(competencia);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCompite(compite.getCompitePK()) != null) {
                throw new PreexistingEntityException("Compite " + compite + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Compite compite) throws NonexistentEntityException, Exception {
        compite.getCompitePK().setIdCompetencia(compite.getCompetencia().getIdCompetencia());
        compite.getCompitePK().setIdDeportista(compite.getDeportista().getIdDeportista());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Compite persistentCompite = em.find(Compite.class, compite.getCompitePK());
            Deportista deportistaOld = persistentCompite.getDeportista();
            Deportista deportistaNew = compite.getDeportista();
            Competencia competenciaOld = persistentCompite.getCompetencia();
            Competencia competenciaNew = compite.getCompetencia();
            if (deportistaNew != null) {
                deportistaNew = em.getReference(deportistaNew.getClass(), deportistaNew.getIdDeportista());
                compite.setDeportista(deportistaNew);
            }
            if (competenciaNew != null) {
                competenciaNew = em.getReference(competenciaNew.getClass(), competenciaNew.getIdCompetencia());
                compite.setCompetencia(competenciaNew);
            }
            compite = em.merge(compite);
            if (deportistaOld != null && !deportistaOld.equals(deportistaNew)) {
                deportistaOld.getCompiteCollection().remove(compite);
                deportistaOld = em.merge(deportistaOld);
            }
            if (deportistaNew != null && !deportistaNew.equals(deportistaOld)) {
                deportistaNew.getCompiteCollection().add(compite);
                deportistaNew = em.merge(deportistaNew);
            }
            if (competenciaOld != null && !competenciaOld.equals(competenciaNew)) {
                competenciaOld.getCompiteCollection().remove(compite);
                competenciaOld = em.merge(competenciaOld);
            }
            if (competenciaNew != null && !competenciaNew.equals(competenciaOld)) {
                competenciaNew.getCompiteCollection().add(compite);
                competenciaNew = em.merge(competenciaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                CompitePK id = compite.getCompitePK();
                if (findCompite(id) == null) {
                    throw new NonexistentEntityException("The compite with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(CompitePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Compite compite;
            try {
                compite = em.getReference(Compite.class, id);
                compite.getCompitePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The compite with id " + id + " no longer exists.", enfe);
            }
            Deportista deportista = compite.getDeportista();
            if (deportista != null) {
                deportista.getCompiteCollection().remove(compite);
                deportista = em.merge(deportista);
            }
            Competencia competencia = compite.getCompetencia();
            if (competencia != null) {
                competencia.getCompiteCollection().remove(compite);
                competencia = em.merge(competencia);
            }
            em.remove(compite);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Compite> findCompiteEntities() {
        return findCompiteEntities(true, -1, -1);
    }

    public List<Compite> findCompiteEntities(int maxResults, int firstResult) {
        return findCompiteEntities(false, maxResults, firstResult);
    }

    private List<Compite> findCompiteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Compite.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Compite findCompite(CompitePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Compite.class, id);
        } finally {
            em.close();
        }
    }

    public int getCompiteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Compite> rt = cq.from(Compite.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
